package me.firstapp.service.weixin.product.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.weixin.product.ProductSku;
import me.firstapp.repository.weixin.product.ProductSkuRepository;
import me.firstapp.service.weixin.product.ProductSkuService;

@Service
public class ProductSkuServiceImpl implements ProductSkuService {

	@Autowired
	private ProductSkuRepository productSkuRepository;

	@Override
	public Page<ProductSku> findProductSkuPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType, Long productId) {
		return productSkuRepository.findProductSkuPage(pageNo, pageSize, searchParams, sortParam, sortType, productId);
	}

	@Override
	public ProductSku executeAdd(Long productId, String skuName, Integer isDefault) {
		ProductSku productSku = productSkuRepository.findByProductIdAndSkuName(productId, skuName);
		if (null != productSku) {
			throw new ServiceException("error", "规格已存在");
		}
		boolean hasDefault = false;
		List<ProductSku> productSkus = productSkuRepository.findByProductId(productId);
		for (ProductSku productSku2 : productSkus) {
			if (productSku2.getIsDefault() == 1) {
				hasDefault = true;
			}
		}
		if (hasDefault == true && isDefault == 1) {
			throw new ServiceException("error", "已有默认规格");
		}
		productSku = new ProductSku();
		productSku.setProductId(productId);
		productSku.setSkuName(skuName);
		productSku.setIsDefault(isDefault);
		productSkuRepository.save(productSku);
		return productSku;
	}

	@Override
	public ProductSku executeUpdate(Long id, String skuName, Integer isDefault) {
		ProductSku productSku = (ProductSku) productSkuRepository.getById(id);
		if (null == productSku) {
			throw new ServiceException("error", "规格不存在");
		}
		if (null != isDefault) {
			boolean hasDefault = false;
			List<ProductSku> productSkus = productSkuRepository.findByProductId(productSku.getProductId());
			for (ProductSku productSku2 : productSkus) {
				if (!productSku2.getId().equals(productSku.getId()) && productSku2.getIsDefault() == 1) {
					hasDefault = true;
				}
			}
			if (hasDefault == true && isDefault == 1) {
				throw new ServiceException("error", "已有默认规格");
			}
			productSku.setIsDefault(isDefault);
		}

		if (StrUtils.isNotNULL(skuName)) {
			productSku.setSkuName(skuName);
		}
		productSkuRepository.update(productSku);
		return productSku;
	}

	@Override
	public ProductSku findById(Long id) {
		return (ProductSku) productSkuRepository.getById(id);
	}

	@Override
	public void executeDelete(Long id) {
		productSkuRepository.deleteById(id);
	}

	@Override
	public List<ProductSku> findByProductId(Long productId) {
		return productSkuRepository.findByProductId(productId);
	}

	@Override
	public ProductSku findByProductIdAndDefault(Long productId) {
		return productSkuRepository.findByProductIdAndDefault(productId);
	}

}
